package com.jack.xiaoer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jack.xiaoer.constant.MyConstant;
import com.jack.xiaoer.dao.CartMapper;
import com.jack.xiaoer.dao.ProductMapper;
import com.jack.xiaoer.dao.ShopMapper;
import com.jack.xiaoer.dao.UserMapper;
import com.jack.xiaoer.dto.CartDTO;
import com.jack.xiaoer.entity.Cart;
import com.jack.xiaoer.entity.Product;
import com.jack.xiaoer.entity.Shop;
import com.jack.xiaoer.entity.User;
import com.jack.xiaoer.service.CartService;
import com.jack.xiaoer.service.error.AccountExistedException;
import com.jack.xiaoer.service.error.ProductNotFoundException;
import com.jack.xiaoer.service.error.ShopNotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

/**
 * Created by Jackaroo Zhang on 2018/12/19.
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Cart add(Optional<CartDTO> cartDtoOptional) {
        if (!cartDtoOptional.isPresent()) {
            return null;
        }

        final CartDTO cartDTO = cartDtoOptional.get();

        User user = userMapper.selectById(cartDTO.getUserId());
        if (user == null) {
            throw new AccountExistedException("Can't find user");
        }
        Shop shop = shopMapper.selectById(cartDTO.getShopId());
        if (shop == null) {
            throw new ShopNotFoundException("Can't find shop");
        }
        Product product = productMapper.selectById(cartDTO.getProductId());
        if (product == null) {
            throw new ProductNotFoundException("Can't find product");
        }

        Cart cart = new Cart();
        BeanUtils.copyProperties(cartDTO, cart);

        cart.setShopName(shop.getName());
        cart.setProductName(product.getName());
        cart.setProductPrice(product.getPrice());
        cart.setProductPictureUrl(product.getPictureUrl());
        if (cart.getChecked() == null)
            cart.setChecked(MyConstant.CHECKED);
        cart.setDeleted(MyConstant.UNDELETED);
        cart.setGmtCreate(Instant.now());
        cart.setGmtModified(Instant.now());

        int row = cartMapper.insert(cart);

        if (row > 0) {
            return cart;
        }
        return null;
    }

    @Override
    public IPage<Cart> listByUserId(Long userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", MyConstant.UNDELETED);

        Page<Cart> page = new Page<>(pageNum, pageSize);
        IPage<Cart> cartPage = cartMapper.selectPage(page, queryWrapper);

        return cartPage;
    }

    @Override
    public Integer countItem(Long userId, Long shopId, Integer checked) {
        if (MyConstant.CHECKED.equals(checked)) {
            return cartMapper.countItem(userId, shopId, MyConstant.CHECKED);
        }

        return 0;
    }

    @Override
    public List<Shop> listShopByUserId(Long userId) {
        return cartMapper.listShopByUserId(userId);
    }

    @Override
    public List<Cart> listByUserIdAndShopId(Long userId, Long shopId, boolean checked) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("shop_id", shopId);
        queryWrapper.eq("deleted", MyConstant.UNDELETED);
        if (checked) {
            queryWrapper.eq("checked", MyConstant.CHECKED);
        }

        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateCheckedByCartId(Long cartId, Integer checkedCode) {
        Cart cart = new Cart();
        cart.setId(cartId);
        cart.setChecked(checkedCode > 0 ? MyConstant.CHECKED : MyConstant.UNCHECKED);

        int row = cartMapper.updateById(cart);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
